import { Model } from 'sequelize';

interface UserModel extends Model { }

const valueToNameStrategy = {
  gender: (value: number) => {
    switch (value) {
      case 1:
        return '男';
      case 2:
        return '女';
      default:
        return '';
    }
  },
  maritalStatus: (value: number) => {
    switch (value) {
      case 0:
        return '未婚';
      case 1:
        return '已婚';
      default:
        return '';
    }
  },
  childrenSituation: (value: number) => {
    switch (value) {
      case 0:
        return '无';
      case 1:
        return '有';
      default:
        return '';
    }
  },
  age: (value: number) => {
    switch (value) {
      case 1:
        return '20-30岁';
      case 2:
        return '31-40岁';
      case 3:
        return '41-50岁';
      case 4:
        return '50岁以上';
      default:
        return '';
    }
  },
  education: (value: number) => {
    switch (value) {
      case 1:
        return '大专及以下';
      case 2:
        return '本科';
      case 3:
        return '硕士';
      case 4:
        return '硕士以上';
      default:
        return '';
    }
  }
};

const valueToName = (key: string, value: number): string => valueToNameStrategy[key](value);

module.exports = (app) => {
  const {
    STRING,
    INTEGER,
    DATE,
    VIRTUAL
  } = app.Sequelize;

  const User = app.model.define('User', {
    id: { type: INTEGER, primaryKey: true, autoIncrement: true },
    uuid: { type: STRING(36), unique: true },
    name: { type: STRING }, // 姓名
    num: { type: STRING }, // 工号
    tel: { type: STRING(11) },
    gender: { type: INTEGER },
    genderName: {
      type: VIRTUAL,
      get(this: UserModel) {
        const rawValue = this.getDataValue('gender');
        return valueToName('gender', rawValue);
      }
    },
    /** 婚姻状态 */
    maritalStatus: { type: INTEGER, field: 'marital_status' },
    maritalStatusName: {
      type: VIRTUAL,
      get(this: UserModel) {
        const rawValue = this.getDataValue('maritalStatus');
        return valueToName('maritalStatus', rawValue);
      }
    },
    /** 子女状况 */
    childrenSituation: { type: INTEGER, field: 'children_situation' },
    childrenSituationName: {
      type: VIRTUAL,
      get(this: UserModel) {
        const rawValue = this.getDataValue('childrenSituation');
        return valueToName('childrenSituation', rawValue);
      }
    },
    age: { type: INTEGER },
    ageName: {
      type: VIRTUAL,
      // eslint-disable-next-line @typescript-eslint/no-unused-vars
      get(this: UserModel) {
        const rawValue = this.getDataValue('age');
        return valueToName('age', rawValue);
      }
    },
    /** 信息是否完善标志位 */
    complateFlag: { type: INTEGER, field: 'complate_flag' },
    /** 文化程度 */
    education: { type: INTEGER },
    educationName: {
      type: VIRTUAL,
      get(this: UserModel) {
        const rawValue = this.getDataValue('education');
        return valueToName('education', rawValue);
      }
    },
    /** 工作单位 */
    workUnit: { type: STRING(255), field: 'work_unit' },
    /** 附属单位 */
    affiliated: { type: STRING(255) },
    /** 工作职位 */
    jobPosition: { type: STRING(255), field: 'job_position' },
    /** 工作岗位 */
    operatingPost: { type: STRING(255), field: 'operating_post' },
    createdAt: { type: DATE, field: 'created_at' },
    updatedAt: { type: DATE, field: 'updated_at' },
  }, {
    tableName: 'users'
  });

  User.associate = () => {
    app.model.User.belongsToMany(app.model.Questionnaire, {
      through: app.model.MergeUserQuestionnaire,
      foreignKey: 'fk_user',
      targetKey: 'id',
    });

    app.model.User.hasMany(app.model.MergeUserQuestionnaire, {
      foreignKey: 'fk_user',
      sourceKey: 'id',
    });
  };

  return User;
};
